#include <errno.h>
#include <fstream>
#include <iostream>
#include <string>

void *buf = malloc(1024);

class Student {
private:
  long int no;
  std::string name;
  double score;

public:
  Student(long int no, const std::string &name, double score)
      : no(no), name(name), score(score){};
  size_t out(char *outChar) {
    return sprintf(outChar, "id: %ld, name: %s, score: %f\n", no, name.c_str(),
                   score);
    ;
  };
};

class StudentManager {
private:
  Student *stus;
  size_t num;

public:
  StudentManager(size_t num, Student *stus) : num(num), stus(stus){};
  void append(const Student &stu) { std::cerr << "no implement" << std::endl; };
  void exportTofile() {
    std::fstream outfile;
    outfile.open("out.txt", std::ios::out);
    if (!outfile) {
      throw errno;
    }

    for (int i = 0; i < num; i++) {
      int n = stus[i].out((char *)buf);
      outfile.write((char *)buf, n);
    }
    outfile.close();
  };
};

int main(int argc, char *argv[]) {
  Student stus[] = {
      Student(1, "lihao", 78),
      Student(2, "fd", 79),
  };

  StudentManager manager = StudentManager(2, stus);
  try {
    manager.exportTofile();
  } catch (int eno) {
    strerror_r(eno, (char *)buf, 1024);
    std::cerr << "exportTofile error: " << (char *)buf << std::endl;
    return 1;
  }

  return 0;
}